package service

import (
	"context"
	"fmt"
	"videosS_rpc/basic/config"
	__ "videosS_rpc/basic/proto"
	"videosS_rpc/handler/model"
	"videosS_rpc/pkg"
)

func (s *Server) MemberAdd(_ context.Context, in *__.MemberAddReq) (*__.MemberAddResp, error) {

	member := &model.Member{

		MemberName:    in.MemberName,
		Price:         in.Price,
		DiscountPrice: in.DiscountPrice,
		Instructions:  in.Instructions,
	}
	err := member.MemberAdd(config.DB)
	if err != nil {
		return nil, fmt.Errorf("会员添加失败")
	}
	pay, err := pkg.Pay(in.MemberName, in.Price)
	if err != nil {
		return nil, err
	}
	return &__.MemberAddResp{
		MemberId: int64(member.ID),
		ApyUrl:   pay,
	}, nil
}

func (s *Server) MemberList(_ context.Context, in *__.MemberListReq) (*__.MemberListResp, error) {

	member := &model.Member{}
	list, err := member.MemberList(config.DB)
	if err != nil {
		return nil, err
	}
	return &__.MemberListResp{
		List: list,
	}, nil
}
